﻿var request = require('request');
var cheerio = require('cheerio');
// var mongoose = require('mongoose');
var newsModel = require('../model.js')





// mongoose.connect('mongodb://127.0.0.1:27017/news');
// var db = mongoose.connection
// db.on('error',function(){
// 	console.log('数据库连接成功')
// })

// var nSchema = mongoose.Schema({
// 	body:String,
// 	time:Date,
// 	lianj:String,
// 	neir:String
// },{colllection:'news'})

// var nModel = mongoose.model('news',nSchema);

var opt = {
		url:'http://tech.chinadaily.com.cn/5b7621d3a310030f813cf45b',
		methods:'get',
		headers:{
			'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
		}
	}


	request('http://tech.chinadaily.com.cn/5b7621d3a310030f813cf45b',function(error,response,body){
		var $ =  cheerio.load(body,{decodeEntities: false})
		
		console.log($('h3').eq(0).text())
		console.log($('.busBox3 p').eq(1).html())
		console.log($('.right-lei3 li').eq(2).text())
		//href 原文连接
		var list = $('.right-lei3').eq(0)
		var lia = list.find('li a')	
		for(var i=0;i<lia.length;i++){
			console.log($('li').eq(i).html())
			var newbb = new newsModel({
				url:$('li').eq(i).html()
			})
			newbb.save();
			newsModel.find(function(err,newInfo){
				console.log(newInfo);
			})
		}
		//时间
		var dis = $('.container').eq(0)
		var di = dis.find('.container-left2 .busBox3')
		for(var j=0;j<di.length;j++){
			console.log($('b').eq(j).html());
			var times = new Date($('b').eq(j).html()).getTime()/1000;
			var newaa = new newsModel({
				postTime:times
			})
			newaa.save();
			newsModel.find(function(err,newInfo){
				console.log(newInfo);
			})
		}
		//标题
		var dis = $('.container').eq(0)
		var di = dis.find('.container-left2 .busBox3')
		for(var t=0;t<di.length;t++){
			console.log($('h3').eq(t).text())
			var newss = new newsModel({
				className:$('h3').eq(t).text()
			})
			newss.save();
			newsModel.find(function(err,newInfo){
				console.log(newInfo);
			})
		}
		
		//内容
		var nei = $('.left-liebiao').eq(0)
		var xia = nei.find('.busBox3')
		// console.log(nei)
		for(var u=0;u<xia.length;u++){
			console.log($('p').eq(u).html())
			var newuu = new newsModel({
				content:$('p').eq(u).html()
			})
			newuu.save();
			newsModel.find(function(err,newInfo){
				console.log(newInfo);
			})
		}
		
	

	})



